package basicGUI;

import main.ChatGUI;
import model.Conversation;
import model.User;

import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JList;

import cs.remoteapp.FileRequest;
import cs.remoteapp.Message;
import cs.remoteapp.TextMessage;

import javax.swing.JTextArea;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

public class BasicConversationView extends BasicMainView{
	private JTextField sendText;
	private JTextArea textArea;
	private User user;
	private Conversation conversation;

	public BasicConversationView(ChatGUI gui, User user, Conversation conversation) {
		super(gui);
		setLayout(null);
		this.conversation= conversation;
		this.user=user;
		
		sendText = new JTextField();
		sendText.setBounds(10, 233, 262, 61);
		add(sendText);
		sendText.setColumns(10);
		
		JButton btnSend = new JButton("Send");
		btnSend.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getGui().getController().getCurrentState().sendText(sendText.getText(), getConversation().getUser());
				textArea.append("Me : "+ sendText.getText() + "\n");
				sendText.setText("");
			}
		});
		btnSend.setBounds(271, 233, 89, 61);
		add(btnSend);
		
		JButton btnNewButton_1 = new JButton("sendFile");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				JFileChooser fileChooser = new JFileChooser();
		        int returnVal = fileChooser.showOpenDialog(null);
				if (returnVal == JFileChooser.APPROVE_OPTION) {
		            File file = fileChooser.getSelectedFile();
		            System.out.println("You want me to send a file");
		            getGui().getController().getCurrentState().sendFile(file, getUser());
				}
			}
		});
		btnNewButton_1.setBounds(361, 233, 79, 61);
		add(btnNewButton_1);
		
		textArea = new JTextArea();
		textArea.setBounds(10, 11, 430, 211);
		add(textArea);
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public Conversation getConversation() {
		return conversation;
	}

	public void setConversation(Conversation conversation) {
		this.conversation = conversation;
	}

	/**
	 * add a text message from the remote user
	 * @param txtMessage
	 */
	public void addMessage(TextMessage txtMessage) {
		textArea.append(user.getName() + " : " + txtMessage.getText() + "\n");
	}

	public void processFileRequest(FileRequest message) {
		int answer = JOptionPane.showConfirmDialog(null, "Do you want to download the file : " + message.getFileName() + " which size is : " + message.getFileSize(), "File request from " + getConversation().getUser().getName(), JOptionPane.YES_NO_OPTION);
		if (answer == JOptionPane.YES_OPTION){
			getGui().getController().getCurrentState().acceptFile(message.getFileName(), getUser());
		}
		else {
			getGui().getController().getCurrentState().refuseFile(message.getFileName(), getUser());			
		}
	}
}
